package com.hbue.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbue.springboot.mapper.TbHistoryMapper;
import com.hbue.springboot.pojo.DTO.TbHistoryDTO;
import com.hbue.springboot.pojo.TbHistory;
import com.hbue.springboot.service.TbHistoryService;
import com.hbue.springboot.vo.QueryParamsVo;
import com.hbue.springboot.vo.WorkerSalaryVo;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
@RequiredArgsConstructor
public class TbHistoryServiceImpl extends ServiceImpl<TbHistoryMapper, TbHistory> implements TbHistoryService {

    private final TbHistoryMapper tbHistoryMapper;

    private static final Logger log = LoggerFactory.getLogger(TbAccountServiceImpl.class);

    /**
     * 单表查询
     * 查询送水历史，有根据时间段查询的功能
     * @return
     */
    @Override
    public Page<TbHistory> list(TbHistoryDTO condition) {
        // 分页
        Page<TbHistory> page = new Page<>(condition.getPageNum(), condition.getPageSize());
        // 根据时间段查询
        QueryWrapper<TbHistory> queryWrapper = new QueryWrapper<>();
        if (condition.getStartTime() != null && condition.getEndTime() != null) {
            queryWrapper.lambda().between(TbHistory::getSendWaterTime, condition.getStartTime(), condition.getEndTime());
        }
        return this.page(page, queryWrapper);

    }

    /**
     * 多表查询
     * 查询送水历史
     * @param condition
     * @return
     */
    @Override
    public Page<TbHistoryDTO> listByPage(TbHistoryDTO condition) {
        log.info("service condition is {}", condition);
        Page<TbHistoryDTO> page = new Page<>(condition.getPageNum(), condition.getPageSize());
        Page<TbHistoryDTO> list = new Page<>();
        if (condition.getStartTime() != null && condition.getEndTime() != null) {
            QueryWrapper<TbHistory> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().between(TbHistory::getSendWaterTime, condition.getStartTime(), condition.getEndTime());
            list = tbHistoryMapper.listByPageBetween(page, queryWrapper);
        } else {
            list = tbHistoryMapper.listByPage(page);
        }
        return list;
    }

    /**
     * 为送水工查询送水历史
     * @param condition
     * @return
     */
    @Override
    public Page<TbHistoryDTO> listForWorkerByPage(TbHistoryDTO condition) {
        Page<TbHistoryDTO> page = new Page<>(condition.getPageNum(), condition.getPageSize());
        Page<TbHistoryDTO> list = new Page<>();
        if (condition.getStartTime() != null && condition.getEndTime() != null) {
            QueryWrapper<TbHistory> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().between(TbHistory::getSendWaterTime, condition.getStartTime(), condition.getEndTime());
            list = tbHistoryMapper.listForWorkerByPageBetween(page, queryWrapper, condition.getUserName());
        } else {
            list = tbHistoryMapper.listForWorkerByPage(page, condition.getUserName());
        }
        return list;
    }

    /**
     * 为客户查询送水历史
     * @param condition
     * @return
     */
    @Override
    public Page<TbHistoryDTO> listForCustByPage(TbHistoryDTO condition) {
        Page<TbHistoryDTO> page = new Page<>(condition.getPageNum(), condition.getPageSize());
        Page<TbHistoryDTO> list = new Page<>();
        if (condition.getStartTime() != null && condition.getEndTime() != null) {
            QueryWrapper<TbHistory> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().between(TbHistory::getSendWaterTime, condition.getStartTime(), condition.getEndTime());
            list = tbHistoryMapper.listForCustByPageBetween(page, queryWrapper, condition.getUserName());
        } else {
            list = tbHistoryMapper.listForCustByPage(page, condition.getUserName());
        }
        return list;
    }

    /**
     * 查询工资
     * @param
     * @return
     */
    @Override
    public Page<WorkerSalaryVo> listSalaryByPage(QueryParamsVo query) {
        Page<WorkerSalaryVo> page = new Page<>(query.getPageNum(), query.getPageSize());
        Page<WorkerSalaryVo> list = new Page<>();
        Map<String, Object> params = query.getParams();
        String beginTime = (String)params.get("beginTime");
        String endTime = (String)params.get("endTime");
        if (beginTime != null && endTime != null) {
            QueryWrapper<TbHistory> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().between(TbHistory::getSendWaterTime, beginTime, endTime);
            list = tbHistoryMapper.listSalaryByPageBetween(page, queryWrapper);
        } else {
            list = tbHistoryMapper.listSalaryByPage(page);
        }
        return list;
    }

}
